package com.example.yuejian23.yuejian_23.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yuejian23.yuejian_23.Data.Mdata;
import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.javaBean.FatieBean;
import com.example.yuejian23.yuejian_23.javaBean.LoginBean;
import com.example.yuejian23.yuejian_23.model.FatieModel;
import com.example.yuejian23.yuejian_23.model.UserModel;
import com.example.yuejian23.yuejian_23.service.FatieService;

import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FatieActivity extends AppCompatActivity {
    private EditText neirong;
    private Button fabiao;
    private ImageView xiangce;
    private String imagePath;
    private static final int IMAGE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fatie);

        initView();
        initEvents();
    }

    private void initView() {
        neirong = (EditText) findViewById(R.id.neirong);
        fabiao = (Button) findViewById(R.id.fabiao);
        xiangce = (ImageView) findViewById(R.id.xiangce);
        xiangce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, IMAGE);
            }
        });
    }

    //进入相册
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //获取图片路径
        if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            imagePath = c.getString(columnIndex);
            showImage(imagePath);
            c.close();

        }
    }
    private void showImage(String imaePath){
        Bitmap bm = BitmapFactory.decodeFile(imaePath);
        xiangce.setImageBitmap(bm);
    }





    private void initEvents() {
        fabiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String nei = neirong.getText().toString();
                if(!nei.equals("")){
                if (imagePath!=null){
                    uploadFile(imagePath,nei);

                }
                else {
                FatieModel fatieModel=new FatieModel();
                Call call=fatieModel.fatie(Mdata.uid,nei);
                Callback<FatieBean> callback = new Callback<FatieBean>() {
                    @Override
                    public void onResponse(Call<FatieBean> call, Response<FatieBean> response) {
                        //String userid = String.valueOf(response.body().getCode());
                        int code = response.body().getCode();
                        if(code == 0)
                        {
                            Toast.makeText(FatieActivity.this,"发表失败",Toast.LENGTH_SHORT).show();
                            //失败处理
                        }else
                        {

                            Toast.makeText(FatieActivity.this,"发表成功",Toast.LENGTH_SHORT).show();
                            finish();
                            //成功处理
                        }
                    }

                    @Override
                    public void onFailure(Call<FatieBean> call, Throwable t) {

                    }
                };
                call.enqueue(callback);
                }

                }
                else {
                    Toast.makeText(FatieActivity.this,"请输入内容",Toast.LENGTH_SHORT).show();

                }

            }
        });
    }



    //传图片
    private void uploadFile(String filePath,String nei) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FatieService service =
                retrofit.create(FatieService.class);

        File file = new File(filePath);
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        Call<ResponseBody> call = service.upload(Mdata.uid,nei,body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call,
                                   Response<ResponseBody> response) {
                try {
                    Toast.makeText(FatieActivity.this,"发表成功",Toast.LENGTH_SHORT).show();
                    Log.e("Upload", "success:"+response.body().string());
                    finish();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("Upload error:", t.getMessage());
            }
        });
    }
}
